//
//  Created by Lee on 15/7/28.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "LEUnitCell.h"

@interface LEUnitCell ()

@property (nonatomic, strong) UIImage *iconImage;// user的头像

@property (nonatomic, strong) NSString *name;// user的名称

@property (nonatomic, strong) UIView *cover;
@property (nonatomic, strong) UIView *progress;
@property (nonatomic, strong) UIView *progressBorder;
@property (nonatomic, strong) UIButton *btn;
@end

@implementation LEUnitCell

- (void)setRatio:(CGFloat)ratio {
    _ratio = ratio;
    
    if (ratio < 0) {
        self.cover.alpha = .0;
    } else {
        self.cover.alpha = .8;
        self.progress.height = self.progressBorder.height * ratio;
        if (self.progress.height == self.progressBorder.height) {
            [UIView animateWithDuration:1.f animations:^{
                self.cover.alpha = 0;
            }];
        }
    }
}

- (void)setImage:(UIImage *)image {
    _image = image;
    
    [self.btn setBackgroundImage:image forState:UIControlStateNormal];
}

- (id)initWithFrame:(CGRect)frame andIconImage:(UIImage *)icon andName:(NSString *)name {
    
    _iconImage = icon ? icon: [UIImage imageNamed:@"未标题-5.png"];
//
    _name = name ? name: @"未标题-5.png";
    
//     _iconImage = [UIImage imageNamed:@"未标题-5.png"];
    self = [super initWithFrame:frame];
    if (self) {
        [self setProperty];
    }
    return self;
}

/*
 *@method 设置UnitCell的属性
 */
- (void)setProperty {
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 4.0;
    
    //删除BTN
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.height, self.height)];
    [self addSubview:btn];
    [btn setBackgroundImage:self.iconImage forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(touched:) forControlEvents:UIControlEventTouchUpInside];
    self.btn = btn;

    //进度条遮盖view
    UIView *cover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.height, self.height)];
    [self addSubview:cover];
    cover.backgroundColor = [UIColor blackColor];
    if ([self.name isEqualToString:DEFAULTCELL]) {
        cover.alpha = 0;
    } else {
        cover.alpha = .8;
    }
    cover.layer.masksToBounds = YES;
    cover.layer.cornerRadius = 4.0;
    self.cover = cover;
    
    //进度条边框
    UIView *progressBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cover.height/1.3, 10)];
    [cover addSubview:progressBorder];
    progressBorder.center = cover.center;
    progressBorder.layer.masksToBounds = YES;
    progressBorder.layer.cornerRadius = 5;
    progressBorder.layer.borderColor = [UIColor whiteColor].CGColor;
    progressBorder.layer.borderWidth = 2;
    progressBorder.backgroundColor = [UIColor whiteColor];
    progressBorder.center = cover.center;
    self.progressBorder = progressBorder;
    
    //进度条
    UIView *progress = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, progressBorder.height)];
    [progressBorder addSubview:progress];
    progress.backgroundColor = [UIColor blackColor];
    self.progress = progress;
    
    //添加按钮的遮盖
    if ([self.name isEqualToString:DEFAULTCELL]) {
        UIView *cover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.height, self.height)];
        [self addSubview:cover];
        cover.backgroundColor = [UIColor clearColor];
    }
}

/*
 *@method UnitCell点击删除（代理告知上层）
 */
- (void)touched:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(unitCellTouched:)])
        [_delegate unitCellTouched:self];
}

@end
